<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus'
import apiMod from '@/apiMod'
import { getAreaObj } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const { setTempParams } = useCommonStore()

const emit = defineEmits(['refresh', 'back'])

type Props = {
	nowRowData?: any
}
let props = withDefaults(defineProps<Props>(), {
	nowRowData: {},
})
console.log('进来的是啥',props.nowRowData)
let BaseFormConfig = {
	inline: false,
	showBtn: true,
	// labelPosition: 'top',
	labelWidth: '100px',
	column: [
		{
			label: '停止时间',
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			prop: 'stopAidDate',
			rules: [{ required: true, message: '请选择停止时间' }],
			span: 24,
		},
		{
			label: '原因类型',
			type: 'select',
			prop: 'stopAidReason',
			propName: 'stopAidReasonLabel',
			dicData: [
				{ label: '死亡', value: 1 },
				{ label: '户口从本市迁出', value: 2 },
				{ label: '被取消低保待遇', value: 3 },
				{ label: '与养老机构服务合同终止', value: 4 },
				{ label: '补助资格逾期未入住养老机构', value: 5 },
				{ label: '其他', value: 6 },
			],
			rules: [{ required: true, message: '请选择原因类型' }],
			control: (e, model) => {
				return {
					stopAidOtherReason: {
						display: e == 6,
					},
				}
			},
			span: 24
		},
		{
			label: '其他原因',
			prop: 'stopAidOtherReason',
			type: 'textarea',
			rules: [{ required: true, message: '请填写其他原因' }],
			span: 24,
		},
	],
}
const formModel: any = ref({})
const formRef = ref()
const onSubmit = (form, done) => {
	let params = {
		id: props.nowRowData.applyElderlyId,
		...form,
	}
	apiMod
		.view_ofsSubsidyApplyElderly_stop_aid(params)
		.then((res) => {
				let caseId = props.nowRowData.tableRel?JSON.parse(props.nowRowData.tableRel):[]
				// 修改停发
				let query = {
					id:editId.value,
					dataContent: {...modelData.value,stopAidDate:form.stopAidDate,stopAidReason:form.stopAidReason,stopAidOtherReason:form.stopAidOtherReason},
					dataResults: {...modelData.value,stopAidDate:form.stopAidDate,stopAidReason:form.stopAidReason,stopAidOtherReason:form.stopAidOtherReason},
					fromId: props.nowRowData.applyNo,
					fromType: caseId[0].value,
					tableTemplateId: caseId[0].value,
					projectId:props.nowRowData.subsidyProjectId,
				}
				apiMod.view_formResults_edit(query).then(res=>{
					ElMessage.success('修改成功')
					emit('refresh', res.data)
					done()
				})
		})
		.catch((err) => {
			done()
		})
}
// 获取表格数据
let editId = ref('')
let modelData = ref({})
const getTableData = async() =>{
	let date = [undefined,undefined]
		if(props.nowRowData.detailBelongDate) {
			date = props.nowRowData.detailBelongDate.split("-")
		}else if(props.nowRowData.belongDate){
			date = props.nowRowData.belongDate.split("-")
		}
		let caseId = props.nowRowData.tableRel?JSON.parse(props.nowRowData.tableRel):[]
		let query = {
			applyMonth:date[1],
			applyYear:date[0],
			formId:props.nowRowData.applyNo,
			formType:caseId[0].result.value,
			id:caseId[0].result,
			tableTemplateId:caseId[0].result.value
		}
		let res = await apiMod.view_formResults_list(query)
		if(res.code==0&&res.data[0]){
			editId.value = res.data[0].id
			modelData.value = res.data[0].dataContent
		}
}
getTableData()

const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
